package com.sdkx.app.preference;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sdkx.app.R;

public class IconListPreference extends ListPreference {
    private Context mContext;
    private int[] mEntryIcons;
    private String mKey;
    private SharedPreferences mPref;
    private SharedPreferences.Editor mEditor;
    private CharSequence[] mEntries;
    private CharSequence[] mEntryValues;
    private int mPosition;

    public IconListPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IconListPreference);
        int iconResId = a.getResourceId(R.styleable.IconListPreference_entryIcons,-1);
        if(iconResId != -1){
            setEntryIcons(iconResId);
        }
        mKey = getKey();
        mPref = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
        mEditor = mPref.edit();
        mEntries = getEntries();
        mEntryValues = getEntryValues();
        String value = mPref.getString(mKey,"");
        mPosition = findIndexOfValue(value);
        if(mPosition!=-1){
            setSummary(mEntries[mPosition]);
            setIcon(mEntryIcons[mPosition]);
        }
        a.recycle();
    }

    private void setEntryIcons(int[] entryIcons){
        mEntryIcons = entryIcons;
    }
    //设置图标：根据icon的id 数组
    public void setEntryIcons(int entryIconsResId) {
        TypedArray icons = getContext().getResources().obtainTypedArray(entryIconsResId);
        int[] ids = new int[icons.length()];
        for (int i = 0; i < icons.length(); i++)
            ids[i] = icons.getResourceId(i, -1);
        setEntryIcons(ids);
        icons.recycle();
    }

    @Override
    protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
        super.onPrepareDialogBuilder(builder);
        PrefIconAdapter adapter = new PrefIconAdapter(mContext);
        builder.setAdapter(adapter, null);
    }

    public class PrefIconAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public PrefIconAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mEntryIcons.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup parent) {
            ViewHolder holder = null;
            if(view ==null){
                holder = new ViewHolder();
                view = mInflater.inflate(R.layout.icon_adapter, parent, false);
                holder.layout = (LinearLayout)view.findViewById(R.id.icon_layout);
                holder.img = (ImageView)view.findViewById(R.id.icon_img);
                holder.info = (TextView)view.findViewById(R.id.icon_info);
                holder.check = (RadioButton)view.findViewById(R.id.icon_check);
                view.setTag(holder);
            }else {
                holder = (ViewHolder)view.getTag();
            }
            holder.img.setBackgroundResource(mEntryIcons[i]);
            holder.info.setText(mEntries[i]);
            holder.check.setChecked(mPosition ==i);
            final ViewHolder fholder = holder;
            final int fpos =i;
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.requestFocus();
                    // 选中效果
                    fholder.layout.setBackgroundColor(Color.CYAN);
                    // 更新mPosition
                    mPosition = fpos;
                    // 更新Summary
                    IconListPreference.this.setSummary(mEntries[fpos]);
                    IconListPreference.this.setIcon(mEntryIcons[fpos]);
                    // 更新该ListPreference保存的值
                    mEditor.putString(mKey, mEntryValues[fpos].toString());
                    mEditor.commit();
                    // 取消ListPreference设置对话框
                    getDialog().dismiss(); } });
            return view;
        }

        private final class ViewHolder{
            ImageView img;
            TextView info;
            RadioButton check;
            LinearLayout layout;
        }
    }
}
